package service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import db.MyHibernateSessionFactory;
import entity.Students;
import service.StudentsDAO;

public class StudentsDAOImpl implements StudentsDAO {

	@Override
	public List<Students> queryAllStudents() {

		Transaction tx = null;
		//创建查询结果list
		List<Students> list = new ArrayList<Students>();
		String sql = "";
		try {
			//获得会话对象
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			//获得事务
			tx = session.beginTransaction();
			//创建查询的HQL语句
			sql = "from Students";
			//创建query对象
			Query query = session.createQuery(sql);
			//list接query内容
			list = query.list();
			//提交事务
			tx.commit();
			//返回list
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return list;
		}finally{
			if (tx!=null) {
				tx = null;
			}
		}
	}

	@Override
	public Students queryStudentsBySid(String sid) {

		Transaction tx = null;
		Students students = null;
		try {
			//获得会话对象
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			//获得事务
			tx = session.beginTransaction();
			students = (Students) session.get(Students.class, sid);
			//提交事务
			tx.commit();
			//返回list
			return students;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return students;
		}finally{
			if (tx!=null) {
				tx = null;
			}
		}
	}

	@Override
	public boolean addStudents(Students students) {
		
		Transaction tx = null;
		String sql = "";
		boolean flag = false;
		students.setSid(getNewSid());
		
		try {
			//获得会话对象
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			//获得事务
			tx = session.beginTransaction();
			session.save(students);
			//提交事务
			tx.commit();
			//返回list
			flag = true;
			return flag;

		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			flag = false;
			return flag;
		}finally{
			if (tx!=null) {
				tx = null;
			}
		}
	}

	@Override
	public boolean updateStudents(Students students) {
		
		Transaction tx = null;
		String sql = "";
		boolean flag = false;
		
		try {
			//获得会话对象
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			//获得事务
			tx = session.beginTransaction();
			session.update(students);
			//提交事务
			tx.commit();
			//返回list
			flag = true;
			return flag;

		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			flag = false;
			return flag;
		}finally{
			if (tx!=null) {
				tx = null;
			}
		}	
	}

	@Override
	public boolean deleteStudents(String sid) {
		
		Transaction tx = null;
		boolean flag = false;
		String sql = "";
		try {
			//获得会话对象
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			//获得事务
			tx = session.beginTransaction();
			//获得
			Students students = (Students) session.get(Students.class, sid);
			//创建查询的HQL语句
//			sql = "from Students";
			session.delete(students);
			//提交事务
			tx.commit();
			//返回list
			flag = true;
			return flag;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return flag;
		}finally{
			if (tx!=null) {
				tx = null;
			}
		}
	}
	
	private String getNewSid(){
		Transaction tx = null;
		String hql = "";
		String sid = null;
		
		try {
			//获得会话对象
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			//获得事务
			tx = session.beginTransaction();
			//获得当前学生的最大编号
			hql = "select max(sid) from Students";
			Query query = session.createQuery(hql);
			sid = (String) query.uniqueResult();
			
			if (sid == null||"".equals(sid)) {
				//给一个默认的最大编号
				sid = "S0000001";
			} else{
				String temp = sid.substring(1);
				int i = Integer.parseInt(temp);
				i = i + 1;
				temp = String.valueOf(i);
				int length = temp.length();
				StringBuffer buffer = new StringBuffer(temp);
				//凑够七位
				for(int j=0;j<7-length;j++)

				{
					buffer.insert(0, "0");
				}
				sid = buffer.insert(0, "S").toString();
			}
			tx.commit();
			return sid;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return null;
		} finally{
			if (tx!=null) {
				tx = null;
			}
		}
	}

}
